<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="班型名称" prop="spuName">
        <el-input
          v-model="queryParams.spuName"
          placeholder="请输入班型名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option
            v-for="item in statusDict"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          @click="openForm('create')"
          v-hasPermi="['train:product-spu:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="班型缩略图" align="center" prop="coverUrl">
        <template #default="scope">
          <el-image
            class="w-20 h-20"
            :src="getOSSURL(scope.row.coverUrl)"
            :preview-src-list="[getOSSURL(scope.row.coverUrl)]"
            fit="cover"
            preview-teleported
          />
        </template>
      </el-table-column>
      <el-table-column label="班型名称" align="center" prop="carType" />
      <el-table-column label="车型" align="center" prop="spuName" />
      <el-table-column
        label="价格(元)"
        align="center"
        prop="salePrice"
        :formatter="priceFormatter"
      />
      <!-- <el-table-column label="总库存" align="center" prop="totalStock" /> -->
      <el-table-column label="班型销量" align="center" prop="salesCount" />
      <!-- <el-table-column label="班型类型" align="center" prop="spuType">
        <template #default="scope">
          <el-tag class="ml-2" :type="typeFormatter(scope.row.spuType).type">{{
            typeFormatter(scope.row.spuType).text
          }}</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-tag class="ml-2" :type="statusFormatter(scope.row.status).type">{{
            statusFormatter(scope.row.status).text
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="gotoSku(scope.row.id)"
            v-hasPermi="['train:product-spu:update']"
          >
            编辑套餐
          </el-button>
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['train:product-spu:update']"
          >
            编辑班型
          </el-button>
          <!-- <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['train:product-spu:delete']"
          >
            删除
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SpuForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts" name="ProductSpu">
import { dateFormatter } from '@/utils/formatTime'
import * as SpuApi from '@/api/client-mall/product/spu'
import { getOSSURL } from '@/utils/oss'
import { statusDict, typeDict } from './common'
import SpuForm from './spu-form.vue'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const router = useRouter() // 路由

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  spuType: 1
})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SpuApi.getSpuPage(queryParams)
    console.log(queryParams)

    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 跳转到 SKU 管理页面 */
const gotoSku = (id: string) => {
  router.push({
    path: '/product/sku',
    query: {
      spuId: id
    }
  })
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: string) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: string) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SpuApi.deleteSpu(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 表格项状态格式化 */
const statusFormatter = (val: number) => {
  const status = statusDict.find((item) => item.value === val)
  return {
    type: status?.type,
    text: status?.label
  }
}

/** 表格项价格式化  分 => 元 */
const priceFormatter = (_row, _column, cellValue, _index) => {
  return cellValue / 100 + '元'
}

/** 表格项商品类型格式化  1 普通商品 2 课程商品 */
const typeFormatter = (val: number) => {
  const type = typeDict.find((item) => item.value === val)
  return {
    type: type?.type,
    text: type?.label
  }
}

/** 初始化 **/
onMounted(getList)
</script>
